<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="机种料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['machineCode', validatorRules.machineCode]" placeholder="请输入机种料号"></a-input>
        </a-form-item>
        <a-form-item label="管控类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['controlType']" :trigger-change="true" dictCode="mcontrol_type" placeholder="请选择管控类型"/>
        </a-form-item>
        <a-form-item label="机种名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['machineName']" placeholder="请输入机种名称"></a-input>
        </a-form-item>
        <a-form-item label="机种规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['machineGague']" placeholder="请输入机种规格"></a-input>
        </a-form-item>
        <a-form-item label="管控料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controlCode', validatorRules.controlCode]" placeholder="请输入管控料号"></a-input>
        </a-form-item>
        <a-form-item label="管控方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['controlManner', validatorRules.controlManner]" :trigger-change="true" dictCode="mcontrol_manner" placeholder="请选择管控方式"/>
        </a-form-item>
        <a-form-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielName']" placeholder="请输入物料名称"></a-input>
        </a-form-item>
        <a-form-item label="物料规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielGague']" placeholder="请输入物料规格"></a-input>
        </a-form-item>
        <a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['produceGrade', validatorRules.produceGrade]" :trigger-change="true" dictCode="" placeholder="请选择生产阶别"/>
        </a-form-item>
        <a-form-item label="管控工序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controlProcess', validatorRules.controlProcess]" placeholder="请输入管控工序"></a-input>
        </a-form-item>
        <a-form-item label="组立顺序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['groupSequence', validatorRules.groupSequence]" placeholder="请输入组立顺序"></a-input>
        </a-form-item>
        <a-form-item label="管控数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controlNum', validatorRules.controlNum]" placeholder="请输入管控数量"></a-input>
        </a-form-item>
        <a-form-item label="加工面别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['processFace', validatorRules.processFace]" :trigger-change="true" dictCode="process_face" placeholder="请选择加工面别"/>
        </a-form-item>
        <a-form-item label="条码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['barcodeRule']" placeholder="请输入条码规则"></a-input>
        </a-form-item>
        <a-form-item label="物料来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['materielSource', validatorRules.materielSource]" :trigger-change="true" dictCode="materiel_source" placeholder="请选择物料来源"/>
        </a-form-item>
        <a-form-item label="管控提示" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['controlClue', validatorRules.controlClue]" placeholder="请输入管控提示"></a-input>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注"/>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  
  export default {
    name: "MesChiefdataMaterielcontrolModal",
    components: { 
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          machineCode: {
            rules: [
              { required: true, message: '请输入机种料号!'},
            ]
          },
          controlCode: {
            rules: [
              { required: true, message: '请输入管控料号!'},
            ]
          },
          controlManner: {
            rules: [
              { required: true, message: '请输入管控方式!'},
            ]
          },
          produceGrade: {
            rules: [
              { required: true, message: '请输入生产阶别!'},
            ]
          },
          controlProcess: {
            rules: [
              { required: true, message: '请输入管控工序!'},
            ]
          },
          groupSequence: {
            rules: [
              { required: true, message: '请输入组立顺序!'},
            ]
          },
          controlNum: {
            rules: [
              { required: true, message: '请输入管控数量!'},
            ]
          },
          processFace: {
            rules: [
              { required: true, message: '请输入加工面别!'},
            ]
          },
          materielSource: {
            rules: [
              { required: true, message: '请输入物料来源!'},
            ]
          },
          controlClue: {
            rules: [
              { required: true, message: '请输入管控提示!'},
            ]
          },
        },
        url: {
          add: "/chiefdata/mesChiefdataMaterielcontrol/add",
          edit: "/chiefdata/mesChiefdataMaterielcontrol/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'machineCode','controlType','machineName','machineGague','controlCode','controlManner','materielName','materielGague','produceGrade','controlProcess','groupSequence','controlNum','processFace','barcodeRule','materielSource','controlClue','notes'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'machineCode','controlType','machineName','machineGague','controlCode','controlManner','materielName','materielGague','produceGrade','controlProcess','groupSequence','controlNum','processFace','barcodeRule','materielSource','controlClue','notes'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>